package com.example.sportrecord;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.sportrecord.SQL.SQLOperation;
import com.example.sportrecord.SQL.User;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {
    TextView Date1,Time1,Month1,start_time,stop_time,all_useTime,user_editext;
    Button NextMonth,LastMonth,day01,day02,day03,day04,day05,day06,day07;
    Button sport_start,sport_stop,insert_data,find_allRecord;
    int year,month,day,hour,minute,second;
    int hour1,minute1,second1;//临时时间
    private static final int msgKey1=1;
    private Context mContext=MainActivity.this;
    int flag=0;//计时标志位
    double time=0;

    User user = new User();
    SQLOperation sqlOperation= new SQLOperation(mContext);



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        new TimeThread().start();

    }
    //创建时间线程
    public class TimeThread extends Thread{
        @Override
        public void run() {
            do{
                try{
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = msgKey1;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }while(true);
        }
    }
    //进行实时动态时间事件处理
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case msgKey1:
                    getDate();
                    Date1.setText(year + "年" + month + "月" + day + "日");
                    Time1.setText(hour + "时" + minute + "分" + second + "秒");
                    break;
                default:
                    break;
            }
        }
    };


    private void initView() {
        Date1 = (TextView)findViewById(R.id.Date);
        Time1 = (TextView)findViewById(R.id.Time);
        NextMonth = (Button)findViewById(R.id.next);
        LastMonth = (Button)findViewById(R.id.last);
        Month1 = (TextView)findViewById(R.id.month);
        sport_start = (Button)findViewById(R.id.sport_start);
        sport_stop = (Button)findViewById(R.id.sport_stop);
        start_time = (TextView)findViewById(R.id.start_time);
        stop_time = (TextView)findViewById(R.id.stop_time);
        all_useTime = (TextView)findViewById(R.id.all_useTime);

        day01 = (Button)findViewById(R.id.day01);
        day02 = (Button)findViewById(R.id.day02);
        day03 = (Button)findViewById(R.id.day03);
        day04 = (Button)findViewById(R.id.day04);
        insert_data = (Button)findViewById(R.id.insert_data);
        find_allRecord = (Button)findViewById(R.id.find_allRecord);
        user_editext = (TextView)findViewById(R.id.user_editext);
        user_editext.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        user_editext.setGravity(Gravity.TOP);
        user_editext.setSingleLine(false);
        user_editext.setHorizontallyScrolling(false);



    }
    private void initEvent() {
        sport_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start_time.setText(hour + "时" + minute + "分" + second + "秒");
                hour1=hour;
                minute1 = minute;
                second1 = second;
                flag++;
            }

        });

        sport_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop_time.setText(hour + "时" + minute + "分" + second + "秒");
                time = (hour-hour1)*60+(minute-minute1)+(second-second1)/60.0;
                flag++;

                all_useTime.setText("合计用时："+ String.format("%.2f",time) + "分钟");
                Toast.makeText(MainActivity.this,String.valueOf(flag),Toast.LENGTH_LONG).show();
            }
        });

        changeBackColor();
        insert_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user.setDate(year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second);
                user.setStartTime(year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second);
                user.setStopTime(year+"/"+month+"/"+day+" "+hour1+":"+minute1+":"+second1);
                user.setAll_usetime(String.format("%.2f",time));
                String str = user_editext.getText().toString();
                user.setExplain(str);

                Toast.makeText(MainActivity.this, user.getDate(), Toast.LENGTH_SHORT).show();

                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();



                sqlOperation.insert(user);


                Toast.makeText(MainActivity.this,"打卡成功2",Toast.LENGTH_LONG).show();

            }
        });


        find_allRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, SportRecord.class);
                startActivity(intent);
            }
        });



    }


    //获取时间函数
    public void getDate(){
        final Calendar calendar = Calendar.getInstance();//获取当前时间
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);
        second = calendar.get(Calendar.SECOND);
    }
    public void changeBackColor(){
        day01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                day01.setBackgroundColor(Color.parseColor("#6200EE"));
            }
        });

        day02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                day02.setBackgroundColor(Color.parseColor("#6200EE"));
            }
        });
        day03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                day03.setBackgroundColor(Color.parseColor("#6200EE"));
            }
        });
        day04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                day04.setBackgroundColor(Color.parseColor("#6200EE"));
            }
        });

    }
}
